package com.yuanlrc.base.controller.admin;

import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.entity.admin.Library;
import com.yuanlrc.base.entity.admin.Library;
import com.yuanlrc.base.service.admin.FacultiesService;
import com.yuanlrc.base.service.admin.LibraryService;
import com.yuanlrc.base.service.admin.OperaterLogService;
import com.yuanlrc.base.service.admin.RoleService;
import com.yuanlrc.base.util.ValidateEntityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 图书馆管理管理控制器
 *
 */
@RequestMapping("/library")
@Controller
public class LibraryController {
@Autowired
    private LibraryService libraryService;
@Autowired
    private FacultiesService facultiesService;
    /**
     * 书屋列表页面
     * @param model
     * @param library
     * @param pageBean
     * @return
     */
    @RequestMapping(value="/list")
    public String list(Model model, Library library, PageBean<Library> pageBean){
        model.addAttribute("title", "书屋列表");
        model.addAttribute("libraryName", library.getLibraryName());
        model.addAttribute("pageBean", libraryService.findList(library, pageBean));
        return "admin/library/list";
    }
    /**
     * 新增书屋页面
     * @param model
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.GET)
    public String add(Model model){
        model.addAttribute("faculties", facultiesService.findAll());
        return "admin/library/add";
    }

    /**
     * 书屋添加表单提交处理
     * @param library
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Library library){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(library);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        //判断书屋名是否存在
        if(libraryService.isExistLibraryName(library.getLibraryName(), 0l)){
            return Result.error(CodeMsg.ADMIN_LIBRARY_EXIST);
        }
        //到这说明一切符合条件，进行数据库新增
        if(libraryService.save(library) == null){
            return Result.error(CodeMsg.ADMIN_LIBRARY_ADD_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 书屋编辑页面
     * @param model
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.GET)
    public String edit(Model model,@RequestParam(name="id",required=true)Long id){
        model.addAttribute("faculties",  facultiesService.findAll());
        model.addAttribute("library", libraryService.find(id));
        return "admin/library/edit";
    }
    /**
     * 编辑书屋信息表单提交处理
     * @param library
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(Library library){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(library);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(library.getId() == null || library.getId().longValue() <= 0){
            return Result.error(CodeMsg.ADMIN_LIBRARY_NO_EXIST);
        }
        if(libraryService.isExistLibraryName(library.getLibraryName(), library.getId())){
            return Result.error(CodeMsg.ADMIN_LIBRARY_EXIST);
        }
        //到这说明一切符合条件，进行数据库保存
        Library findById = libraryService.find(library.getId());
        //讲提交的书屋信息指定字段复制到已存在的library对象中,该方法会覆盖新字段内容
        BeanUtils.copyProperties(library, findById, "id","createTime","updateTime");
        if(libraryService.save(findById) == null){
            return Result.error(CodeMsg.ADMIN_LIBRARY_EDIT_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 删除书屋
     * @param id
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
        try {
            libraryService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_LIBRARY_DELETE_ERROR);
        }
        return Result.success(true);
    }

}
